package com.homni.iomix_xair.activity;

import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.FrameLayout;
import android.widget.Toast;

import com.homni.iomix_xair.R;

public class HomeActivity extends AppCompatActivity {

    private Toolbar toolbar;
    private FrameLayout mContentFrame;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);

        initView();
        initListener();
        setDefaultFragment();

    }

    private void initView(){

        mContentFrame = (FrameLayout) findViewById(R.id.layout_content_frame);
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        // App Logo
        toolbar.setLogo(R.mipmap.ic_launcher);
        // Title
        toolbar.setTitle("Mixer View");
        // Sub Title
        toolbar.setSubtitle("LR Mix");

        setSupportActionBar(toolbar);
    }

    private void initData(){

    }

    private void initListener(){
        // Menu item click 的監聽事件一樣要設定在 setSupportActionBar 才有作用
        toolbar.setOnMenuItemClickListener(onMenuItemClick);
    }

    private void setDefaultFragment() {
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        HomeFragment mHomeFragment = HomeFragment.newInstance();
        transaction.replace(R.id.layout_content_frame, mHomeFragment);
        transaction.commit();
    }

    private Toolbar.OnMenuItemClickListener onMenuItemClick = new Toolbar.OnMenuItemClickListener() {
        @Override
        public boolean onMenuItemClick(MenuItem menuItem) {
            String msg = "";
            switch (menuItem.getItemId()) {

                case R.id.action_first:
                    msg += "Click action_first";
                    break;
                case R.id.action_second:
                    msg += "Click action_second";
                    break;
                case R.id.action_third:
                    msg += "Click action_third";
                    break;
                case R.id.action_fourth:
                    msg += "Click action_fourth";
                    break;
                case R.id.action_fifth:
                    msg += "Click action_fifth";
                    break;
                case R.id.action_sixth:
                    msg += "Click action_sixth";
                    break;
            }

            if(!msg.equals("")) {
                Toast.makeText(HomeActivity.this, msg, Toast.LENGTH_SHORT).show();
            }
            return true;
        }
    };

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        //为了让Toolbar的Menu有作用，这边的代码不可以拿掉
        getMenuInflater().inflate(R.menu.menu_home, menu);
        return true;
    }

}
